﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;

namespace PhoobearLibs.Media.Imaging.Crop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.drawCanvas.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(drawCanvas_PreviewMouseLeftButtonDown);            
            this.drawCanvas.MouseLeftButtonUp += new MouseButtonEventHandler(drawCanvas_MouseLeftButtonUp);
            this.drawCanvas.MouseMove += new MouseEventHandler(drawCanvas_MouseMove);
        }

        void drawCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pscol.Clear();            
        }

        PathSegmentCollection pscol = new PathSegmentCollection();

        void drawCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {                
                Point pt = e.GetPosition(this);
                PathSegment seg = new LineSegment(new Point(pt.X, pt.Y), false);
                pscol.Add(seg);                
            }
        }

        void drawCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (pscol.Count > 0)
            {
                Path p = new Path();

                PathGeometry pg = new PathGeometry();
                p.Data = pg;
                pg.FillRule = FillRule.Nonzero;

                PathFigureCollection figs = new PathFigureCollection();
                pg.Figures = figs;

                //닫힌 Path를 형성함
                PathSegmentCollection pscol2 = pscol.Clone();
                PathSegment last = pscol2.Last();
                pscol2.Insert(0, last);

                PathFigure fig = new PathFigure();
                fig.Segments = pscol2;
                fig.IsClosed = true;
                figs.Add(fig);

                p.Fill = Brushes.Azure;

                canvFig.Children.Add(p);
                                
                pg.Transform = new TranslateTransform(-this.imgSample.Margin.Left - 10, -this.imgSample.Margin.Top - 10);
                this.imgSample.Clip = pg;              

                this.drawCanvas.Strokes.Clear();

                RectangleGeometry rectGeo = new RectangleGeometry(new Rect(this.imgSample.Margin.Left, this.imgSample.Margin.Top, this.imgSample.Width, this.imgSample.Height));

                Path p3 = new Path();

                Geometry.

                //http://msdn.microsoft.com/en-us/library/system.windows.media.combinedgeometry.aspx?appId=Dev10IDEF1&l=EN-US&k=k(%22SYSTEM.WINDOWS.MEDIA.COMBINEDGEOMETRY.%23CTOR%22);k(TargetFrameworkMoniker-%22.NETFRAMEWORK&k=VERSION=V4.0%22);k(DevLang-CSHARP)&rd=true
            
                PathFigureCollection figs2 = new PathFigureCollection();
                pg.Figures = figs;


                PathGeometry rt = new PathGeometry(
                
                p3.Data
                

                
                
                CombinedGeometry cmb = new CombinedGeometry(GeometryCombineMode.Exclude, rectGeo.GetOutlinedPathGeometry(), pg);                

                

                Path p2 = new Path();
                p2.Fill = Brushes.SteelBlue;                
                p2.Data = cmb;
                p2.StrokeThickness = 3;
                p2.Stroke = Brushes.RosyBrown;

                //this.imgSample.Measure(new Size(1000, 1000));
                //RenderTargetBitmap rtb = new RenderTargetBitmap((int)this.imgSample.ActualWidth, (int)this.imgSample.ActualHeight, 96, 96, PixelFormats.Default);
                //rtb.Render(this.imgSample);

                //Image newImg = new Image();
                //newImg.Width = 300;
                //newImg.Height = 200;
                //newImg.Source = new BitmapImage(new Uri("sample.jpg", UriKind.Relative));
                //newImg.Clip = cmb;

                this.canvCutted.Children.Add(p2);
            }
        }

        private void grdBase_Loaded(object sender, RoutedEventArgs e)
        {
            //Test2();
        }

        private void Test1()
        {
            Image img = new Image();
            BitmapImage b = new BitmapImage();

            b.BeginInit();
            b.UriSource = new Uri(@"sample.jpg", UriKind.Relative);
            b.EndInit();

            img.Source = b;

            img.Width = b.Width;
            img.Height = b.Height;

            this.grdBase.Children.Add(img);


            //////////////////////////////////////

            RectangleGeometry r = new RectangleGeometry();
            r.Rect = new Rect(100, 60, 200, 100);

            img.Clip = r as Geometry;

            //////////////////////////////////////

            Image img2 = new Image();
            img2.Source = CaptureScreen(img, 100, 100);

            this.grdBase.Children.Add(img2);

            ///////////////////////////////////////

            Path line = new Path();
            line.Data = new PathGeometry();
        }

        private void Test2()
        {
            Path line = new Path();
            RectangleGeometry rect = new RectangleGeometry();
            rect.Rect = new Rect(100, 60, 500, 400);
            line.Data = rect;
            
            BitmapImage b = new BitmapImage();
            b.BeginInit();
            b.UriSource = new Uri(@"sample.jpg", UriKind.Relative);
            b.EndInit();
            
            ImageBrush brush = new ImageBrush(b);
            brush.Stretch = Stretch.None;            
            brush.Transform = new TranslateTransform(150, 70);

            line.Fill = brush;

            this.grdBase.Children.Add(line);
        }
        
        private static BitmapSource CaptureScreen(Visual target, double dpiX, double dpiY)
        {
            if (target == null)
            {
                return null;
            }
            Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                            (int)(bounds.Height * dpiY / 96.0),
                                                            dpiX,
                                                            dpiY,
                                                            PixelFormats.Pbgra32);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(target);
                ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
            }
            rtb.Render(dv);
            return rtb;
        }
    }
}
